#include <tws_debugging.h>
#include <xwritelogexport.h>
#include <assert.h>
#include "xdataudpdconf.h"

static char Udpd_Conf_Path[] = "/usr/infomat/udpd.conf";
static char Udpd_Conf_Section[] ="";
static char Udpd_Conf_Key_Ip[] ="IPV4ADDRESS";
static char Udpd_Conf_Key_Gateway[] ="GATEWAY";
static char Udpd_Conf_Key_Udp_Port[] ="UDPPORT";
static char Udpd_Conf_Key_Timeout[] = "TIMEOUT";
static char Udpd_Conf_Key_Protocol[] ="PROTOCOL";
static char Udpd_Conf_Key_Mac[] = "MACADDR";

XDataUdpdConf::XDataUdpdConf()
{
	this->clearStUdpdConf();
	assert (0==m_iniFile.open(Udpd_Conf_Path));

	char tmp[32] = {0};
	if(0!=m_iniFile.readInt (Udpd_Conf_Section, Udpd_Conf_Key_Timeout,m_udpdConf.timeout))
		LOG_E("[Error]: Get timeout Failed");
	if(0!=m_iniFile.readInt (Udpd_Conf_Section, Udpd_Conf_Key_Udp_Port,m_udpdConf.port))
		LOG_E("[Error]: Get port Failed");
	
	if(0!=m_iniFile.readString (Udpd_Conf_Section, Udpd_Conf_Key_Ip, tmp , 32))
		LOG_E("[Error]: Get ip Failed");
	m_udpdConf.ip = std::string(tmp);
	memset (tmp, 0x00, 32);
	
	if(0!=m_iniFile.readString (Udpd_Conf_Section, Udpd_Conf_Key_Gateway, tmp,  32))
		LOG_E("[Error]: Get gateway Failed");
	m_udpdConf.gateway = std::string(tmp);
	memset (tmp, 0x00, 32);
	
	if(0!=m_iniFile.readString (Udpd_Conf_Section, Udpd_Conf_Key_Protocol,	tmp,  32))
		LOG_E("[Error]: Get protocol Failed");

	m_udpdConf.protocol = std::string(tmp);
	memset (tmp, 0x00, 32);

	if(0!=m_iniFile.readString (Udpd_Conf_Section, Udpd_Conf_Key_Mac, tmp,  32))
		LOG_E("[Error]: Get mac Failed");
	m_udpdConf.mac = std::string(tmp);
	m_iniFile.flush();
	m_iniFile.close();
	//this->debugOutput();
}

/* *****************************************************
 * @brief  	:(+) clean memory
 * ****************************************************/
XDataUdpdConf::~XDataUdpdConf()
{

}
/* *****************************************************
 *  @brief	:(+) return boot layout data
 * ***************************************************/
const STUdpdConf& XDataUdpdConf::getUdpdConf() const
{
	return m_udpdConf;
}

/* ***************************************************
 * @brief 	: (#)clear strucutre STUdpConf
 * ***************************************************/
void XDataUdpdConf::clearStUdpdConf()
{
	m_udpdConf.ip.clear();
	m_udpdConf.gateway.clear();
	m_udpdConf.port = 0;
	m_udpdConf.protocol.clear();
	m_udpdConf.mac.clear();
}

void XDataUdpdConf::debugOutput ()
{
	DEBUG("ip\t:%s",		m_udpdConf.ip.c_str());
	DEBUG("gateway\t:%s",	m_udpdConf.gateway.c_str());
	DEBUG("protocol:\t:%s", m_udpdConf.protocol.c_str());
	DEBUG("mac:\t:%s",		m_udpdConf.mac.c_str());
	DEBUG("port\t:%02d",  	m_udpdConf.port);
	DEBUG("timeout:\t:%02d",m_udpdConf.timeout);
}

std::string XDataUdpdConf::getIp() const
{
	return m_udpdConf.ip;
}

int XDataUdpdConf::getPort() const
{
	return m_udpdConf.port;
}

